package org.jeecg.modules.xxgx.fpgl.eventListener;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.config.TenantContext;
import org.jeecg.modules.innerApi.common.requestDto.InvoiceParam;
import org.jeecg.modules.innerApi.common.responseDto.FileStreamRtn;
import org.jeecg.modules.innerApi.common.responseDto.InvoiceRtn;
import org.jeecg.modules.xxgx.common.enums.FplxdmEnum;
import org.jeecg.modules.xxgx.fpgl.entity.CsXxFpxx;
import org.jeecg.modules.xxgx.fpgl.event.FpDownloadEvent;
import org.jeecg.modules.xxgx.fpgl.event.vo.FpDownloadEventVo;
import org.jeecg.modules.xxgx.fpgl.service.ICsXxFpxxService;
import org.jeecg.modules.xxgx.fpjf.param.FpjfSmsParam;
import org.jeecg.modules.xxgx.fpjf.param.FpjfYxParam;
import org.jeecg.modules.xxgx.fpjf.service.ICsXxJfjlService;
import org.jeecg.modules.xxgx.sjtb.service.ISjtbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 生成PDF事件监听器
 */
@Component
@Slf4j
public class FpDownloadEventListener {
    @Autowired
    private ICsXxFpxxService fpxxService;

    @Autowired
    private ICsXxJfjlService csXxJfjlService;
    @Autowired
    private ISjtbService sjtbService;

    @Async()
    @EventListener
    public void CreatePdfEvent(FpDownloadEvent event) {
        FpDownloadEventVo eventVo=(FpDownloadEventVo)event.getSource();
        InvoiceParam invoiceParam = eventVo.getInvoiceParam();
        InvoiceRtn invoiceRtn = eventVo.getInvoiceRtn();
        log.info("发票版式自动下载", invoiceRtn.getData().getFphm());
        //设置当前任务线程中的租户信息
        TenantContext.setTenant(invoiceParam.getKpzd().getTenantId().toString());

        String fplxdm = invoiceParam.getFplxdm();

        if(!FplxdmEnum.sdzp.getValue().equals(fplxdm)
            &&!FplxdmEnum.sdpp.getValue().equals(fplxdm)
            &&!FplxdmEnum.dzpp.getValue().equals(fplxdm)
            &&!FplxdmEnum.dzzp.getValue().equals(fplxdm)){
            log.info("非电子票，不能下载版式文件！");
            return;
        }

        FileStreamRtn<CsXxFpxx> fileStreamRtn = new FileStreamRtn<>();
        //下载ofd
        try {
            fileStreamRtn = fpxxService.download(eventVo.getId(), "1");
        }catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        //下载pdf
        try {
            if(fileStreamRtn == null || fileStreamRtn.getData() == null || StringUtils.isEmpty(fileStreamRtn.getData().getPdfdz())) {
                fileStreamRtn = fpxxService.download(eventVo.getId(), "2");
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        //数电下载xml
        if(FplxdmEnum.sdzp.getValue().equals(fplxdm)
                || FplxdmEnum.sdpp.getValue().equals(fplxdm)) {
            //下载xml
            try {
                if(fileStreamRtn == null || fileStreamRtn.getData() == null || StringUtils.isEmpty(fileStreamRtn.getData().getXmldz())) {
                    fileStreamRtn = fpxxService.download(eventVo.getId(), "3");
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }

        //数据同步至服务端
        try{
            sjtbService.pushFpxx(eventVo.getId());
        }catch (Exception e){
            log.error("数据同步至服务端出错:" + e.getMessage(),e);
        }
        //推送Email
        try {
            if (StringUtils.isNotBlank(invoiceParam.getKhlxyx())) {
                FpjfYxParam fpjfYxParam = new FpjfYxParam();
                fpjfYxParam.setFpxxid(eventVo.getId());
                csXxJfjlService.yxjf(fpjfYxParam);
            }
        }catch (Exception e){
            log.error("推送Email出错:" + e.getMessage(),e);
        }

        //推送短信
        try {
            if (StringUtils.isNotBlank(invoiceParam.getKhlxfs())) {
                FpjfSmsParam fpjfSmsParam = new FpjfSmsParam();
                fpjfSmsParam.setFpxxid(eventVo.getId());
                csXxJfjlService.dxjf(fpjfSmsParam);
            }
        }catch (Exception e){
            log.error("推送短信出错:" + e.getMessage(),e);
        }
    }
}
